package 最长公共子序列1;

import java.util.Scanner;

public class Main {
    static int N = 1010;
    static int[][] f = new int[N][N];

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        char[] s1 = in.next().toCharArray();
        char[] s2 = in.next().toCharArray();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]);
                if (s1[i - 1] == s2[j - 1]) {
                    f[i][j] = f[i - 1][j - 1] + 1;
                }
            }
        }
        System.out.println(f[n][m]);
    }
}
